package com.atguigu.juc.study.chapter03.cf.otherapi;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author : leimingchao
 * @date Date : 2022年06月11日 22:07
 */
public class CompletableFutureAPIDemo {

    public static void main(String[] args) {

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "abc";
        });

        // System.out.println(completableFuture.get());
        //过时不候，如果在指定时间内没有获取结果，抛异常
        // System.out.println(completableFuture.get(2L, TimeUnit.SECONDS));
        // System.out.println(completableFuture.join());

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // System.out.println(completableFuture.getNow("xxx"));

        System.out.println(completableFuture.complete("completableValue") + "\t" + completableFuture.join());
    }
}
